package com.stoneframe.controller.manageAuthorize;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.stoneframe.controller.base.BaseGenericController;
import com.stoneframe.entity.other.TreeEntity;
import com.stoneframe.entity.other.TreeGridEntity;
import com.stoneframe.service.BaseService;
import com.stoneframe.service.manageAuthorize.SysModuleButtonService;
import com.stoneframe.util.baseApp.AppHelper;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseJson.JsonHelper;
import com.stoneframe.util.baseJson.TreeJsonHelper;

@Controller
@RequestMapping(value="sysModuleButton")
public class SysModuleButtonController extends BaseGenericController {
	
	@Resource(name="sysModuleButtonService")
	private SysModuleButtonService sysModuleButtonService;

	@Override
	public BaseService getService() {
		// TODO Auto-generated method stub
		return sysModuleButtonService;
	}
	
	/**
	 * 树GridJson
	 */
	@ResponseBody
	@RequestMapping(value="treeGridJson",produces= "text/plain;charset=UTF-8")
	public Object treeGridJson(){
		logBefore(logger, className+"treeGridJson");
		
		PageData pd=this.getPageData();
		List<PageData> list=null;
		List<TreeGridEntity> treeGridList=new ArrayList();
		try{
			list=sysModuleButtonService.listAll(pd);
			for(PageData item : list){
				TreeGridEntity treeGrid = new TreeGridEntity();
                Boolean hasChildren =false;
                for(PageData subItem:list){
                	if(item.getString("buttonId").equals(subItem.getString("parentId"))){
                		hasChildren=true;
                		break;
                	}
                }
                treeGrid.setId(item.getString("buttonId"));
                treeGrid.setHasChildren(hasChildren);
                treeGrid.setParentId(item.getString("parentId"));
                treeGrid.setExpanded(true);
                treeGrid.setEntityJson(JsonHelper.toJsonStr(item));
                treeGridList.add(treeGrid);
			}
			
			logAfter(logger);
			return AppHelper.returnObject(pd, TreeJsonHelper.treeGridToJson(treeGridList));
		}catch(Exception e){
			logger.error(e.getMessage(),e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	
	/**
	 * list转树GridJson
	 */
	@ResponseBody
	@RequestMapping(value="listToTreeGridJson",produces= "text/plain;charset=UTF-8")
	public Object listToTreeGridJson(String moduleButtonJson){
		logBefore(logger, className+"listToTreeGridJson");
		
		List<TreeGridEntity> treeGridList=new ArrayList();
		try{
			List<Map<String, Object>> data=new ObjectMapper().readValue(moduleButtonJson, List.class);
			for(Map<String, Object> item : data){
				TreeGridEntity treeGrid = new TreeGridEntity();
                Boolean hasChildren =false;
                for(Map<String, Object> subItem:data){
                	if(item.get("buttonId").equals(subItem.get("parentId"))){
                		hasChildren=true;
                		break;
                	}
                }
                String buttonId=item.get("buttonId").toString().equals("")?this.get32Guid():item.get("buttonId").toString();
                item.put("buttonId", buttonId);
                treeGrid.setId(buttonId);
                treeGrid.setHasChildren(hasChildren);
                treeGrid.setParentId(item.get("parentId").toString());
                treeGrid.setExpanded(true);
                treeGrid.setEntityJson(JsonHelper.toJsonStr(item));
                treeGridList.add(treeGrid);
			}
			
			logAfter(logger);
			return AppHelper.returnObject(new PageData(), TreeJsonHelper.treeGridToJson(treeGridList));
		}catch(Exception e){
			logger.error(e.toString(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		
		
	}

	/**
	 * list转树GridJson
	 */
	@ResponseBody
	@RequestMapping(value="listToTreeJson",produces= "text/plain;charset=UTF-8")
	public Object listToTreeJson(String moduleButtonJson){
		logBefore(logger, className+"listToTreeJson");
		List<TreeEntity> treeList=new ArrayList();
		try{
			List<Map<String, Object>> data=new ObjectMapper().readValue(moduleButtonJson, List.class);
			for(Map<String, Object> item:data){
				TreeEntity tree = new TreeEntity();
                Boolean hasChildren =false;
                for(Map<String, Object> subItem:data){
                	if(item.get("buttonId").equals(subItem.get("parentId"))){
                		hasChildren=true;
                		break;
                	}
                }
                String buttonId=item.get("buttonId").toString().equals("")?this.get32Guid():item.get("buttonId").toString();
                tree.setId(buttonId);
                tree.setText(item.get("buttonName").toString());
                tree.setValue(buttonId);
                tree.setIsexpand(true);
                tree.setComplete(true);
                tree.setHasChildren(hasChildren);
                tree.setParentId(item.get("parentId").toString());
                
                treeList.add(tree);
			}
			
			logAfter(logger);
			return AppHelper.returnObject(new PageData(), TreeJsonHelper.treeToJson(treeList, "0"));
		}catch(Exception e){
			logger.error(e.toString(), e);
			return AppHelper.errorObject(e.getMessage());
		}
	}
	
	/**
	 * 复制按钮
	 */
	@ResponseBody
	@RequestMapping(value="copyForm",produces= "text/plain;charset=UTF-8")
	public Object copyForm(String keyValue, String moduleId){
		logBefore(logger, className+"copyForm");
		
		try{
			PageData buttonData=sysModuleButtonService.findById(new PageData("keyValue",keyValue));
			buttonData.put("buttonId", this.getTimeUuid());
			buttonData.put("moduleId", moduleId);
			buttonData.put("parentId", "0");
			sysModuleButtonService.save(buttonData);
			
			logAfter(logger);
			return AppHelper.successObject("复制成功。");
		}catch(Exception e){
			logger.error(e.toString(), e);
			return AppHelper.errorObject(e.getMessage());
		}
		
	}

}
